dojo.provide('ph.osc.Receiver')

dojo.require('ph.event.Event');
ph.require('com.illposed.osc.OSCPortIn');
ph.require('com.illposed.osc.OSCListener');

dojo.declare('ph.osc.Receiver', null, {
  constructor: function(port) {
    this._port = new com.illposed.osc.OSCPortIn(port);
  },
  
  start: function() {
    this._port.startListening();
  },
  
  stop: function() {
    this._port.stopListening();
  },
  
  subscribe: function(topic, context, method) {
    this._port.addListener(topic, new JavaAdapter(com.illposed.osc.OSCListener, {
      acceptMessage: function(time, message) {
        var rawArgs = message.getArguments();
        var args = [];
        for (var i = 0; i < rawArgs.length; i++) args[i] = rawArgs[i];
        dojo.publish(topic, args);
      }
    }));
    
    return dojo.subscribe(topic, context, method);
  }
});